package org.jeecg.modules.patient.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.ZoeException;
import org.jeecg.modules.charge.entity.ComCardInfo;
import org.jeecg.modules.charge.service.IComCardInfoService;
import org.jeecg.modules.patient.entity.PatBasicInfo;
import org.jeecg.modules.patient.entity.PatExtendInfo;
import org.jeecg.modules.patient.mapper.PatExtendInfoMapper;
import org.jeecg.modules.patient.mapper.PatBasicInfoMapper;
import org.jeecg.modules.patient.service.IPatBasicInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 病人基础信息
 * @Author: jeecg-boot
 * @Date:   2022-05-13
 * @Version: V1.0
 */
@Service
public class PatBasicInfoServiceImpl extends ServiceImpl<PatBasicInfoMapper, PatBasicInfo> implements IPatBasicInfoService {

	@Autowired
	private PatBasicInfoMapper patBasicInfoMapper;
	@Autowired
	private PatExtendInfoMapper patExtendInfoMapper;
	@Autowired
	private IComCardInfoService comCardInfoService;

	@Override
	public void register(ComCardInfo cardInfo, PatBasicInfo basicInfo) {
		if (StringUtils.isEmpty(basicInfo.getCertificateId())){
			throw new ZoeException("该身份证不可为空");
		}
		//判断重复
		PatBasicInfo oldData = baseMapper.getOneByCertificateId(basicInfo.getCertificateId());
		if (oldData!=null){
			throw new ZoeException("该身份证已经存在数据");
		}
		//获取id
		if (save(basicInfo)){
			basicInfo = baseMapper.getOneByCertificateId(basicInfo.getCertificateId());
			//激活卡
			cardInfo.setPatientId(basicInfo.getId());
			comCardInfoService.activate(cardInfo);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		patExtendInfoMapper.deleteByMainId(id);
		patBasicInfoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			patExtendInfoMapper.deleteByMainId(id.toString());
			patBasicInfoMapper.deleteById(id);
		}
	}

}
